use strict;
use warnings;

package JasPerl::JSTL::Core::CatchTag;
use parent qw(JasPerl::TagExt::JspTag);

sub set_var {
    $_[0]->{var} = $_[1];
}

sub do_tag {
    my $self = shift;
    my $body = $self->get_jsp_body();

    eval {
        $body->invoke() if $body;
    };

    # TODO: wrap $@ in Throwable
    my $e = $@;
    if (defined $self->{var}) {
        my $context = $self->get_jsp_context();

        if ($e) {
            $context->set_attribute($self->{var}, $e);
        } else {
            $context->remove_attribute($self->{var});
        }
    }
}

1;
